let user = 1
function first() {
  console.log(user)
}
function second() {
  let user = 2
  first()
}
second()




var add = function (x) {
  var sum = 1;
  var tmp = function (x) {
    sum = sum + x;
    return tmp;
  }
  tmp.toString = function () {
    return sum;
  }
  return tmp;
}
console.log(add(1)(2)(3))



function m1() {
  var x = 1;
  return function () {
    console.log(++x);
  }
}

m1()();   //2
m1()();   //2
m1()();   //2

var m2 = m1();
m2();   //2
m2();   //3
m2();   //4



function fun(n, o) {
  console.log(o);
  return {
    fun: function (m) {
      return fun(m, n);
    }
  };
}
var a = fun(0);  //undefined
a.fun(1);  //0  
a.fun(2);  //0  
a.fun(3);  //0  
var b = fun(0).fun(1).fun(2).fun(3);   //undefined  0  1  2
var c = fun(0).fun(1);
c.fun(2);
c.fun(3);  //undefined  0  1  1




function fn() {
  var arr = [];
  for (var i = 0; i < 5; i++) {
    arr[i] = function () {
      return i;
    }
  }
  return arr;
}
var list = fn();
for (var i = 0, len = list.length; i < len; i++) {
  console.log(list[i]());
}  //5 5 5 5 5




function fn() {
  var arr = [];
  for (var i = 0; i < 5; i++) {
    arr[i] = (function (i) {
      return function () {
        return i;
      };
    })(i);
  }
  return arr;
}
var list = fn();
for (var i = 0, len = list.length; i < len; i++) {
  console.log(list[i]());
}  //0 1 2 3 4